# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 波士顿房价预测（1欠拟合）.py
# @Author: dongguangwen
# @Date  : 2025-02-06 11:17
# 0.导入工具包
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error

# 1.准备数据
np.random.seed(22)
x = np.random.uniform(-3, 3, size=100)
# print(x)
y = 0.5 * x ** 2 + np.random.normal(0, 1, size=100)
print(y)

# 2.模型训练
model = LinearRegression()
X = x.reshape(-1, 1)  # 线性回归模型需要二维数组
model.fit(X, y)

# 3.模型预测
y_pred = model.predict(X)
print(y_pred)
print(mean_squared_error(y, y_pred))  # 计算均方误差

# 4.展示效果
plt.scatter(X, y)
plt.plot(X, y_pred, color='red')
plt.show()

"""
[ 2.68991269 -0.15896455 -1.0657469   1.77691179  2.2169277   1.44760438
  1.95468165  1.43909861  0.15853376  1.32780734  4.86511909  0.39702949
  2.63475246 -0.14604323  2.36460552  1.62061024  0.83996403  3.84213349
  0.38401779  1.60688722  1.07599777  0.65478898  0.9188096  -0.64424027
  1.59171645 -0.51865919  1.34359201  2.92620657  2.80123304  4.40659537
  0.87811453  1.10113724  6.1923925   0.855368    2.53145803  2.20814387
  0.50914933  3.56598319 -1.52802892 -0.22965108  0.68003709  4.97404497
 -0.75143267  2.88409744  5.60301458  0.99887944  2.62802974  2.46495793
  3.66571442 -1.85804906  0.83140672  0.86165142  0.33581663  0.06046012
  2.05133222  0.89686498  0.570795    4.81741428 -0.24435866  1.68542272
  2.49369326  2.83636823 -0.23086228  2.26822132  2.33381088  0.67749307
  1.59425991  2.65328755  3.70118035  3.9285876   1.95288399  2.60958529
  3.87293405  3.76537917  0.40451931  3.28686949  0.78030988  4.27240723
  2.19597273 -0.5814483   1.44486224  0.43852349 -0.27671756  2.04600828
  4.32094833 -0.20276307  2.15265487  1.69291982  0.10829686  5.84398733
  0.48910081  5.08461625  0.35116797  2.44363252  0.07344753  1.08868863
  1.41343863  2.96338708  4.35563861  1.45501813]
[2.08457816 1.77832256 1.84685839 1.35517802 2.12638696 1.93840767
 2.0150007  1.54364854 2.07119003 1.4081198  2.30644396 1.68918485
 1.40612989 1.48305333 2.10626685 2.31136028 1.45285201 1.24460006
 1.5314345  1.98468521 1.45739296 1.54681289 1.88424584 1.62865333
 1.83899273 1.66330758 1.53065252 2.19281923 1.28334122 1.20979121
 1.55892719 1.42693556 2.28565167 2.11899173 1.33752845 1.48324189
 1.74872034 2.1743929  1.77540463 1.74010802 1.96934399 2.26706116
 1.82764305 2.21076563 2.26756718 1.55061234 1.36637731 2.17789767
 2.17357451 1.78798599 2.09679835 1.44211495 1.63738742 1.82111999
 1.21747747 1.52356849 2.07306655 2.13050434 1.71433608 1.96692717
 1.29649278 2.22155879 1.96619494 2.0726104  1.49250716 1.38952453
 1.48365395 2.11924061 1.35599494 2.31206472 1.91735786 1.24985032
 1.26259715 2.29240777 1.56385333 2.13428626 1.68141843 2.04684562
 2.14130138 1.75379508 1.67731279 1.79146932 1.74863394 1.95534725
 1.32455884 2.03595872 1.35192086 1.60624593 1.53671926 2.2841079
 1.58145688 1.19809323 1.75045179 1.27283177 1.82414333 2.09402954
 1.4171962  1.41291602 2.25328369 1.43238177]
2.7365298290204287
"""